package com.zrrd.thread._08_pool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author WanJl
 * @version 1.0
 * @title MyThreadPoolDemo02
 * @description
 * @create 2023/10/29 16:55
 */
public class MyThreadPoolDemo02 {
    public static void main(String[] args) {
        /*
        static ExecutorService newCachedThreadPool()   创建一个默认的线程池
        static newFixedThreadPool(int nThreads)	    创建一个指定最多线程数量的线程池
         */
        //1、创建线程池，并制定池中最大线程数量
        //设置当前线程池最大线程数量为50，不是初始数量为50
        ExecutorService executorService = Executors.newFixedThreadPool(5);

        executorService.submit(() -> {
            System.out.println(Thread.currentThread().getName() + "在执行...");
        });
        executorService.submit(() -> {
            System.out.println(Thread.currentThread().getName() + "在执行...");
        });
        executorService.submit(() -> {
            System.out.println(Thread.currentThread().getName() + "在执行...");
        });
        executorService.submit(() -> {
            System.out.println(Thread.currentThread().getName() + "在执行...");
        });

        //将executorService对象强转成ThreadPoolExecutor对象
        //ThreadPoolExecutor类中有计算当前线程池中线程数量的方法。
        ThreadPoolExecutor tpe=(ThreadPoolExecutor)executorService;

        int poolSize = tpe.getPoolSize();
        System.out.println(poolSize);
        executorService.shutdown();
    }
}
